var gulp = require ('gulp');
var gulpSequence = require ('gulp-sequence'); //任务串行
var clean = require ('gulp-clean'); //清除文件
var rename = require ('gulp-rename'); //文件改名
var minifyCss = require ('gulp-minify-css'); //压缩CSS为一行；
var concat = require ('gulp-concat'); //多个文件合并为一个；
var htmlmin = require ('gulp-htmlmin'); //压缩html
var jshint = require ('gulp-jshint'); //js代码检查
var uglify = require ('gulp-uglify'); //js代码压缩
// var babel = require("gulp-babel"); //Babel 的插件。
var stylish = require ('jshint-stylish'); //高亮提示
// var map = require("map-stream");
var htmlhint = require ('gulp-htmlhint'); //检查html
var htmlhint_stylish = require ('htmlhint-stylish');
var sass = require ('gulp-sass'); // 读取用于编译Sass的插件

//路径配置表
var path = {
  src: 'src/',
  dest: 'dest/',
  gZip: 'gZip/',
  resource: '*.{html,map,gzip,json,png,jpg,jpeg,gif,bmp,ico,eot,svg,ttf,woff}',
  rev: {
    html: 'rev/html/',
    css: 'rev/css/',
    js: {
      appMinJs: 'rev/js/appMinJs',
      assetsJs: 'rev/js/assetsJs',
      componentJs: 'rev/js/componentJs',
    },
  },
};

//文件路径
gulp.task ('pathJs', function () {
  var aa = pathStr (filePath.css, '!');
  aa.push (config.src + config.assets + '**/*.css');
  console.log (aa);

  return gulp.src (aa, {base: 'src'}).pipe (gulp.dest (config.dest));
});

//清除临时文件
gulp.task ('clean', function () {
  return gulp.src ([path.dest, 'rev/']).pipe (clean ());
});

//js合并angular库文件
gulp.task ('concatJs', function () {
  return gulp
    .src ([
      './node_modules/angular/angular.min.js',
      './node_modules/angular-ui-router/release/angular-ui-router.min.js',
    ])
    .pipe (concat ('./angularJs.js'))
    .pipe (gulp.dest ('./gulpDist/js'));
});

//js合并业务逻辑文件,并压缩
gulp.task ('concatJs1', function () {
  return gulp
    .src (['./connectJS/1.js', './connectJS/2.js'])
    .pipe (concat ('./connectJS.js'))
    .pipe (gulp.dest ('./gulpDist/js'))
    .pipe (rename ('./connectJS.min.js'))
    .pipe (uglify ())
    .pipe (gulp.dest ('./gulpDist/js'));
});

//css合并
gulp.task ('concatCss', function () {
  return gulp
    .src ('./src/style/css/main.scss')
    .pipe (
      sass ({
        outputStyle: 'expanded',
      })
    )
    .pipe (gulp.dest ('./gulpDist/css'))
    .pipe (rename ('index.min.css'))
    .pipe (
      sass ({
        outputStyle: 'compressed',
      })
    )
    .pipe (gulp.dest ('./gulpDist/css'));
});

//监听文件变化
gulp.task ('watch', function () {
  gulp.watch ('./src/style/**/*.scss', ['concatCss']);
});

// gulp.task (
//   'default',
//   gulpSequence ('clean', 'copyFile', 'minCss', 'appMinJs', 'angularMinJs')
// );

// gulp.task ('default', gulpSequence ('concatJs', 'concatJs1'));
gulp.task ('default', gulpSequence ('concatCss', 'watch'));
